#include "string.h"

char* strcpy(char* dest, const char* src)
{
    char* ret = dest;
    while (*src) 
    {
        *dest++ = *src++;
    }
    *dest = '\0';
    return ret;
}

char* strcat(char* dest, const char* src)
{
    char* ret = dest;
    while (*dest)
    {
        dest++;
    }
    while (*src)
    {
        *dest++ = *src++;
    }
    *dest = '\0';
    return ret;

}

size_t strlen(const char* str)
{
    size_t len = 0;
    while (*str++)
    {
        len++;
    }
    return len;
}

int strcmp(const char* str1, const char* str2)
{
    while (*str1 && *str2 && *str1 == *str2)
    {
        str1++;
        str2++;
    }
    return *str1 - *str2;
}
char* strchr(const char* str, int ch)
{
    while (*str && *str != ch)
    {
        str++;
    }
    return *str == ch ? (char*)str : NULL;
}


char* strrchr(const char* str, int ch)
{
    size_t len = strlen(str);
    size_t i = len - 1;
    while (i >= 0 && str[i] != ch)
    {
        i--;
    }
    return i >= 0 ? (char*)str + i : NULL;
}

int memcmp(const void* lhs, const void* rhs, size_t count)
{
    const char* l = (const char*)lhs;
    const char* r = (const char*)rhs;
    while (count--)
    {
        if (*l != *r)
        {
            return *l - *r;
        }
        l++;
        r++;
    }
    return 0;
}

void* memset(void* desc, int ch, size_t count)
{
    char* ptr = (char*)desc;
    while (count--)
    {
        *ptr++ = ch;
    }
    return desc;
}

void* memcpy(void* dest, const void* src, size_t count)
{
    char * dest1 = (char*)dest;
    char * src1 = (const char*)src;
    while (count--)
    {
        *dest1++ = *src1++;
    }
    return dest;
    
}
void* memchr(const void* ptr, int ch, size_t count)
{
    const char* p = (const char*)ptr;
    while (count--)
    {
        if (*p == ch)
        {
            return (void*)p;
        }
        p++;
    }
    return NULL;
}